'use strict';

module.exports = app => {
    class BbsController extends app.Controller {
        async bbs(){
            const data = await this.service.bbs.bbs();
            await this.ctx.render("/bbs/bbs.html",{data});
        };

        //社区帖子详情
        async postdetail(){
            if ('GET' == this.ctx.request.method) {
                const params = this.ctx.request.query;
                const data = await this.service.bbs.postdetail(params);
                const comment = await this.service.comment.getPostComment(params);
                await this.ctx.render("/bbs/post-detail.html", {data, comment});
            }else{
                const params = this.ctx.request.body;
                const post = await this.service.comment.setPostComment(params);
                this.success();
            }
        }

        //查看更多社区分类
        async bbsmore(){
            const params = this.ctx.request.query;
            const data = await this.service.bbs.bbsmore();
            await this.ctx.render("/bbs/bbs-more.html",{data,params});

        }

        //根据社区分类编号查看社区详情
        async bbscont(){
            const params = this.ctx.request.query;
            const data = await this.service.bbs.bbscont(params);
            //最新帖子
            const newpost = await this.service.post.getNewPost(params,1);
            //热门帖子
            const hotpost = await this.service.post.getHotPost(params);
            //提问帖子
            const forumpost = await this.service.post.getNewPost(params,2);
            await this.ctx.render("/bbs/bbs-cont.html",{data,newpost,hotpost,forumpost});
        }
        //我的收藏
        async attention(){
            const data = await this.service.bbs.collection();
            const post = await this.service.bbs.collectionpost(2);
            const answer = await this.service.bbs.collectionpost(3);
            await this.ctx.render("/bbs/attention.html",{data,post,answer});
        }

        //问答帖子详情
        async askquestions(){
            if('GET' == this.ctx.request.method){
                const params = this.ctx.request.query;
                const data = await this.service.bbs.postdetail(params);
                const comment = await this.service.comment.getPostComment(params);
                await this.ctx.render("/bbs/ask-questions.html",{data,comment});
            }else{
                const params = this.ctx.request.body;
                const post = await this.service.comment.setPostComment(params);
                this.success();
            }

        }
        //查看更多问答帖子
        async askquestionslist(){
            const data = await this.service.bbs.getanswerlist();
            await this.ctx.render("/bbs/ask-questionslist.html",{data});
        }

        //搜索页面
        async search(){
            const params =this.ctx.request.query;
            const data = await this.service.bbs.search(params);
            await this.ctx.render("/bbs/search.html",{data});
        }


        //发布帖子页面
        async posting(){
            if('GET' == this.ctx.request.method){
                await this.ctx.render("/bbs/posting.html");
            }else{
                const params = this.ctx.request.body;
                const post = await this.service.post.setPost(params);
                this.success();
            }
        }

        async asking(){
            if('GET' == this.ctx.request.method){
                await this.ctx.render("/bbs/asking.html");
            }else{
                const params = this.ctx.request.body;
                const post = await this.service.post.setPost(params);
                this.success();
            }
        }


    }
    return BbsController;
};

